credit_full_lm=lm(Balance~Income+Limit+Rating+Cards,data=Credit)
summary(credit_full_lm)
##
## Call:
## lm(formula = Balance ~ Income + Limit + Rating + Cards, data = Credit)
##
## Residuals:
## Min 1Q Median 3Q Max
## -243.94 -113.62 -36.59 49.40 542.63
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -495.38408 32.24720 -15.362 <2e-16 ***
## Income -7.67493 0.37846 -20.280 <2e-16 ***
## Limit 0.12931 0.05306 2.437 0.0152 *
## Rating 2.02159 0.79454 2.544 0.0113 *
## Cards 11.01175 7.07876 1.556 0.1206
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 162.1 on 395 degrees of freedom
## Multiple R-squared: 0.877, Adjusted R-squared: 0.8757
## F-statistic: 704 on 4 and 395 DF, p-value: < 2.2e-16
Based on the p-values, Income and Rating are significantly correlated to the balance
credit_full_reduced_lm=lm(Balance~Income+Rating,data=Credit)
summary(credit_full_reduced_lm)
##
## Call:
## lm(formula = Balance ~ Income + Rating, data = Credit)
##
## Residuals:
## Min 1Q Median 3Q Max
## -278.57 -112.69 -36.21 57.92 575.24
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -534.81215 21.60270 -24.76 <2e-16 ***
## Income -7.67212 0.37846 -20.27 <2e-16 ***
## Rating 3.94926 0.08621 45.81 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 162.9 on 397 degrees of freedom
## Multiple R-squared: 0.8751, Adjusted R-squared: 0.8745
## F-statistic: 1391 on 2 and 397 DF, p-value: < 2.2e-16
Credit %>%
ggplot(aes(x=Income, y=Balance))+
geom_point(shape=1)+
geom_smooth(method="lm",se=FALSE,col="cyan2")
## `geom_smooth()` using formula = 'y ~ x'
Credit %>%
ggplot(aes(x=Rating, y=Income))+
geom_point(shape=1)+
geom_smooth(method="lm",se=FALSE,col="darkorange1")
## `geom_smooth()` using formula = 'y ~ x'
install.packages("plotly")
## Installing package into '/home/gafur/R/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
adv_3d=plot_ly(Credit,x=~Income,y=~Rating,z=~Balance)
adv_3d %>% add_markers()
x_values=min(Credit$Income):max(Credit$Income)
y_values=min(Credit$Rating):max(Credit$Rating)
xy_grid=expand.grid(Income=x_values,Rating=y_values)
yhat=matrix(predict(credit_full_reduced_lm,xy_grid),
nrow=length(x_values),ncol=length(y_values))
adv_3d %>% add_markers(color=I("black")) %>%
add_surface(z=~t(yhat),
x=~x_values,
y=~y_values,
colorscale="Rainbow",
colorbar = list(title = "Predicted Sales"))
y=Credit$Balance
yhat=predict(credit_full_reduced_lm,Credit)
res=y-yhat
plot(yhat,res,xlab="Fitted values",ylab="Residuals")